<?php
namespace app\api\controller;

use extend\Tool;
use think\Cache;
use think\Controller;
use think\Request;
use app\common\model\RegisterAct;

class Index extends Controller
{
	public function initUEdit(){
		include_once ROOT_PATH.'extend/uedit/controller.php';
	}
	
	/**
	 * 图片上传
	 * @Version 1.0
	 * @Author  masky
	 *
	 */
	public function uploadImage()
	{
		if(input('type')=='base64'){
			$this->uploadImageByBase64();
		}else{
			$files=Request::instance()->file('file');
			if($files)
			{
				$fileUrl=Tool::uploadImage($files);
				if($fileUrl)
				{
					$this->success('上传成功','',$fileUrl);
				}else
				{
					$this->error('上传失败');
				}
			}else
			{
				$this->error('参数错误');
			}
		}
		
	}
	
	/**
	 * 图片上传
	 * @Version 1.0
	 * @Author  masky
	 *
	 */
	public function editUploadImage()
	{
		$files=Request::instance()->file();
		if($files)
		{
			$result=Tool::uploadImageArray($files);
			if($result)
			{
				return json_encode(['errno'=>0,'data'=>$result]);
			}else
			{
				return json_encode(['errno'=>1,'data'=>[]]);
			}
		}else
		{
			return json_encode(['errno'=>1,'data'=>[]]);
		}
		
	}
	/**
	 * 图片上传 base64
	 * @Version 1.0
	 * @Author  masky
	 *
	 */
	public function uploadImageByBase64()
	{
		$base64_image_content=input('file');
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
			$type = $result[2];
			$path=config('upload_temp_path').DS.date("Ymd").DS;
			Tool::makeDir($path);
			$fileName = $path.uniqid().".{$type}";
			if (file_put_contents($fileName, base64_decode(str_replace($result[1], '', $base64_image_content)))){
				$fileName=str_replace('\\','/',$fileName);
				$root=str_replace('\\','/',ROOT_PATH).'public';
				$fileUrl= str_replace($root,'',$fileName);
				$this->success('上传成功','',$fileUrl);
			}
		}
		$this->error('参数错误');
	}
	/**
	 * 清理缓存
	 * @Version 1.0
	 * @Author  masky
	 */
	public function refreshCache(){
		Cache::clear();
		$this->success('清理成功');
	}
	
	public function register(){
		$reg=new RegisterAct();
		$result=$reg->addLog(input());
		if($result)
		{
			$this->success('报名成功！请耐心等待工作人员联系~');
		}else
		{
			$this->error($reg->getError());
		}
	}
}
